package com.wyq.new_springboot.config.redis;

import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * redis的api
 * @Author weiyaqiang
 * @create 2021/4/2 10:25
 */
@Component
public class RedisServiceImpl implements RedisService {
       //@Resource默认是根据name进行装配的，不是spring自带的
       // @Autowired默认是根据类型进行装配的 是spring包下的
    @Resource
    private RedisTemplate<String,String> redisTemplate;


    @Override
    public void set(String key, String value) {
        redisTemplate.opsForValue().set(key,value);
    }

    @Override
    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    @Override
    public void expire(String key, long expire) {
       redisTemplate.expire(key,expire, TimeUnit.SECONDS);
    }

    @Override
    public boolean remove(String key) {
        Boolean delete=false;
        try{
            delete = redisTemplate.delete(key);
        }catch (Exception e){
            System.out.println("redis出现异常");
        }
        return delete;
    }
}
